﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： WeChartTrailRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-07-31
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;
using YouWei.Utility;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///微信用户行为轨迹 仓储层
    /// </summary>	
    public class WeChartTrailRepository : BaseRepository<WeChartTrail>
    {

        #region 构造

        public WeChartTrailRepository() : base() { }

        public WeChartTrailRepository(string connectionString) : base(connectionString) { }

        public WeChartTrailRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion


        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<WeChartTrail> Pages(int pageIndex, int pageSize, WeChatTrailSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT * FROM [WeChartTrail] T1 WITH(NOLOCK) WHERE 1=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }

            sb.Append(" ORDER BY T1.WeChartTrailID DESC");
            return Pages(pageIndex, pageSize, sb.ToString(), dparams);
        }

        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(WeChatTrailSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (!string.IsNullOrEmpty(model.OpenID))
            {
                sb.Append(" AND T1.OpenID=@OpenID");
                dparams.Add("OpenID", model.OpenID, System.Data.DbType.String);
            }

            if (!string.IsNullOrEmpty(model.UnionID))
            {
                sb.Append(" AND T1.UnionID=@UnionID");
                dparams.Add("UnionID", model.UnionID, System.Data.DbType.String);
            }

            if (!string.IsNullOrEmpty(model.TrailIdentity))
            {
                sb.Append(" AND T1.TrailIdentity=@TrailIdentity");
                dparams.Add("TrailIdentity", model.TrailIdentity, System.Data.DbType.String);
            }

            if (model.TrailType > 0)
            {
                sb.Append(" AND T1.TrailType=@TrailType");
                dparams.Add("TrailType", model.TrailType, System.Data.DbType.Int32);
            }

            if (model.ActionType > 0)
            {
                sb.Append(" AND T1.TrailType=@ActionType");
                dparams.Add("ActionType", model.ActionType, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.BTrailDate))
            {
                sb.Append(" AND T1.TrailDate>@BTrailDate");
                dparams.Add("BTrailDate", EConvert.ToDateTime(model.BTrailDate).Date, System.Data.DbType.DateTime);
            }

            if (!string.IsNullOrEmpty(model.ETrailDate))
            {
                sb.Append(" AND T1.TrailDate<@ETrailDate");
                dparams.Add("ETrailDate", EConvert.ToDateTime(model.ETrailDate).Date.AddDays(1), System.Data.DbType.DateTime);
            }
        }

    }
}